package com.example.wangzh.developkit.control;

import android.os.Handler;
import android.view.DragEvent;
import android.view.View;
import android.widget.ListView;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.adapter.CountDownAdapter;
import com.example.wangzh.developkit.adapter.MessageAdapter;
import com.example.wangzh.developkit.base.BaseActivity;
import com.example.wangzh.developkit.model.Message;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wangzh on 2017/10/20.
 */

public class SwipeActivity extends BaseActivity {
    @BindView(R.id.ptrl)
    PullToRefreshListView ptrl;

    List<Message> messages;
    MessageAdapter messageAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_swipe;
    }

    @Override
    protected void initView() {
        messages = new ArrayList<>();
        initMessage();
        messageAdapter = new MessageAdapter(this, messages);
        ptrl.setAdapter(messageAdapter);
        ptrl.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                initMessage();
                messageAdapter.notifyDataSetChanged();
                ptrl.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ptrl.onRefreshComplete();
                    }
                },500);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                addMessage();
                messageAdapter.notifyDataSetChanged();
                ptrl.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ptrl.onRefreshComplete();
                    }
                },500);
            }
        });
    }

    private void initMessage() {
        messages.clear();
        messages.add(new Message("你好"));
        messages.add(new Message("欢迎你"));
        messages.add(new Message("这里是测试消息"));
        messages.add(new Message("下拉可刷新"));
        messages.add(new Message("上拉加载更多"));
        messages.add(new Message("侧拉可删除"));
    }

    private void addMessage() {
        messages.add(new Message("更多消息1"));
        messages.add(new Message("更多消息2"));
        messages.add(new Message("更多消息3"));
        messages.add(new Message("更多消息4"));
        messages.add(new Message("更多消息5"));
        messages.add(new Message("更多消息6"));
        messages.add(new Message("更多消息7"));
    }


}
